<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>tr</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_001_014_2334">&nbsp;</a>NAME</h4><blockquote>
tr - translate characters
</blockquote><h4><a name = "tag_001_014_2335">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

tr <b>[</b>-cs<b>]</b><i> string1 string2</i>

tr -s<b>[</b>-c<b>]</b><i> string1</i>

tr -d<b>[</b>-c<b>]</b><i> string1</i>

tr -ds<b>[</b>-c<b>]</b><i> string1 string2</i>
</code>
</pre>
</blockquote><h4><a name = "tag_001_014_2336">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>tr</i>
utility copies the standard input to the standard output with
substitution or deletion of selected characters.
The options specified and the
<i>string1</i>
and
<i>string2</i>
operands control translations that occur while copying characters
and
single-character collating elements.
</blockquote><h4><a name = "tag_001_014_2337">&nbsp;</a>OPTIONS</h4><blockquote>
The
<i>tr</i>
utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> .
<p>
The following options are supported:
<dl compact>

<dt><b>-c</b>
<dd>Complement the set of characters specified by
<i>string1</i>.
See the EXTENDED DESCRIPTION section.

<dt><b>-d</b>
<dd>Delete all occurrences of input
characters that are specified by
<i>string1</i>.

<dt><b>-s</b>
<dd>Replace instances of repeated characters with a single character,
as described in the EXTENDED DESCRIPTION section.

</dl>
</blockquote><h4><a name = "tag_001_014_2338">&nbsp;</a>OPERANDS</h4><blockquote>
The following operands are supported:
<dl compact>

<dt><i>string1</i><dd>
<dt><i>string2</i><dd>Translation control strings.
Each string represents a set of characters to
be converted into an array of characters
used for the translation.
For a detailed description of how the strings
are interpreted, see the EXTENDED DESCRIPTION section.

</dl>
</blockquote><h4><a name = "tag_001_014_2339">&nbsp;</a>STDIN</h4><blockquote>
The standard input can be any type of file.
</blockquote><h4><a name = "tag_001_014_2340">&nbsp;</a>INPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_2341">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>
The following environment variables affect the execution of
<i>tr</i>:
<dl compact>

<dt><i>LANG</i><dd>Provide a default value for the internationalisation variables
that are unset or null.
If
<i>LANG</i>
is unset or null, the corresponding value from the
implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the
utility will behave as if none of the variables had been defined.

<dt><i>LC_ALL</i><dd>
If set to a non-empty string value,
override the values of all the other internationalisation variables.

<dt><i>LC_COLLATE</i><dd>
Determine the locale for the
behaviour of range expressions and equivalence classes.


<dt><i>LC_CTYPE</i><dd>
Determine the
locale for the interpretation of sequences of bytes of text data
as characters (for example, single-
versus multi-byte characters in arguments)
and the behaviour of character classes.

<dt><i>LC_MESSAGES</i><dd>
Determine the locale that should be used to affect
the format and contents of diagnostic
messages written to standard error.

<dt><i>NLSPATH</i><dd>
Determine the location of message catalogues
for the processing of
<i>LC_MESSAGES .
</i>
</dl>
</blockquote><h4><a name = "tag_001_014_2342">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_001_014_2343">&nbsp;</a>STDOUT</h4><blockquote>
The
<i>tr</i>
output is identical to the input, with the exception
of the specified transformations.
</blockquote><h4><a name = "tag_001_014_2344">&nbsp;</a>STDERR</h4><blockquote>
Used only for diagnostic messages.
</blockquote><h4><a name = "tag_001_014_2345">&nbsp;</a>OUTPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_2346">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>
The operands
<i>string1</i>
and
<i>string2</i>
(if specified)
define two arrays of characters.
The constructs in the following list
can be used to specify characters or single-character collating elements.
If any of the constructs result in
multi-character collating elements,
<i>tr</i>
will exclude, without a diagnostic,
those multi-character elements from the resulting array.
<dl compact>

<dt><i>character</i><dd>
Any character not described by one of the conventions below
represents itself.

<dt><b>\</b><i>octal</i><dd>Octal sequences can be used to represent characters
with specific coded values.
An octal sequence consists of a backslash
followed by the
longest sequence of one-,
two- or three-octal-digit
characters (01234567).
The sequence causes
the character whose encoding
is represented by the
one-, two- or three-digit
octal integer to be placed into the array.
If the size of a byte on the system is greater than nine bits,
the valid escape sequence used to represent
a byte is implementation-dependent.
Multi-byte characters require multiple,
concatenated escape sequences of this type,
including the leading \ for each byte.

<dt><b>\</b><i>character</i><dd>
The backslash-escape sequences in
the table in
the <b>XBD</b> specification, <a href="../xbd/notation.html"><b>File Format Notation</b>&nbsp;</a> 
(\\,
\a,
\b,
\f,
\n,
\r,
\t,
\v)
are supported.
The results of using any other character,
other than an octal digit, following the backslash are unspecified.

<dt><i>c-c</i><dd>Represents the range of collating elements between the range endpoints,
inclusive,
as defined by the current setting of the
LC_COLLATE locale category.
The starting endpoint must precede the second endpoint in
the current collation order.
The characters or collating elements in the range
are placed in the array in ascending collation sequence.


<dt>[:<i>class</i>:]<dd>Represents all characters belonging to the defined character class,
as defined by the current setting of the
LC_CTYPE locale category.
The following character class names will be accepted when specified in
<i>string1</i>:
<pre>
<dl compact><dt> <dd>
<table <tr valign=top><th align=left>alnum
<th align=left>blank
<th align=left>digit
<th align=left>lower
<th align=left>punct
<th align=left>upper
<tr valign=top><td align=left>alpha
<td align=left>cntrl
<td align=left>graph
<td align=left>print
<td align=left>space
<td align=left>xdigit
</table>
</dl>
</pre>
<p>
In addition, character class expressions of the form
<b>[:</b><i>name</i><b>:]</b>
are recognised in those locales where the
<i>name</i>
keyword has been given a
<b>charclass</b>
definition in the
LC_CTYPE category.
<p>
When both the
<b>-d</b>
and
<b>-s</b>
options are specified,
any of the character class names will be accepted in
<i>string2</i>.
Otherwise, only character class names
<b>lower</b>
or
<b>upper</b>
are valid in
<i>string2</i>
and then only if the corresponding character class
(upper
and
<b>lower</b>,
respectively)
is specified in the same relative position in
<i>string1</i>.
Such a specification is interpreted
as a request for case conversion.
When
[:lower:]
appears in
<i>string1</i>
and
[:upper:]
appears in
<i>string2,</i>
the arrays will contain the characters from the
<b>toupper</b>
mapping in the LC_CTYPE category of the current locale.
When
[:upper:]
appears in
<i>string1</i>
and
[:lower:]
appears in
<i>string2,</i>
the arrays will contain the characters from the
<b>tolower</b>
mapping in the LC_CTYPE category of the current locale.
The first character from each mapping pair will be in the array for
<i>string1</i>
and the second character from
each mapping pair will be in the array for
<i>string2</i>
in the same relative position.
<p>
Except for case conversion, the characters specified by a
character class expression are placed in the array in an
unspecified order.
<p>
If the name specified for
<i>class</i>
does not define a valid
character class in the current locale, the behaviour is undefined.
<p>
<dt>[=<i>equiv</i>=]<dd>
Represents all characters or collating elements belonging to the
same equivalence class as
<i>equiv ,</i>
as defined by the current setting of the
LC_COLLATE locale category.
An equivalence class expression is allowed only in
<i>string1</i>,
or in
<i>string2</i>
when it is being used by the combined
<b>-d</b>
and
<b>-s</b>
options.
The characters belonging to the equivalence class are placed
in the array in an unspecified order.
<p>
<p>
<dt>[<i>x*n</i>]<dd>Represents
<i>n</i>
repeated occurrences of the character
<i>x .</i>
Because this expression is used to map multiple characters
to one, it is only valid when it occurs in
<i>string2</i>.
If
<i>n</i>
is omitted or is zero, it is interpreted as large enough to extend the
<i>string2</i>-based
sequence to the length of the
<i>string1</i>-based
sequence.
If
<i>n</i>
has a leading zero,
it is interpreted as an octal value.
Otherwise, it is interpreted as a decimal value.
<p>
</dl>
<p>
When the
<b>-d</b>
option is not specified:
<ul>
<p>
<li>
Each input character found in the array specified by
<i>string1</i>
is replaced by the character in the same
relative position in the array specified by
<i>string2</i>.
When the array specified by
<i>string2</i>
is shorter that the one
specified by
<i>string1</i>,
the results are unspecified.
<p>
<li>
If the
<b>-c</b>
option is specified,
the complements of the characters specified by
<i>string1</i>
(the set of all characters in the current character set,
as defined by the current setting of LC_CTYPE,
except for those actually specified in the
<i>string1</i>
operand)
are placed in the array in
ascending collation sequence, as defined by the current setting
of LC_COLLATE.
<p>
<li>
Because the order in which characters specified by character class
expressions or equivalence class expressions is undefined,
such expressions should only be used if the intent is to map
several characters into one.
An exception is case conversion,
as described previously.
<p>
</ul>
<p>
When the
<b>-d</b>
option is specified:
<ul>
<p>
<li>
Input characters found in the array specified by
<i>string1</i>
will be deleted.
<p>
<li>
When the
<b>-c</b>
option is specified with
<b>-d</b>,
all characters except those specified by
<i>string1</i>
will be deleted.
The contents of
<i>string2</i>
will be ignored, unless the
<b>-s</b>
option is also specified.
<p>
<li>
The same string cannot be used for both the
<b>-d</b>
and the
<b>-s</b>
option; when both options are specified, both
<i>string1</i>
(used for deletion)
and
<i>string2</i>
(used for squeezing)
are required.
<p>
</ul>
<p>
When the
<b>-s</b>
option is specified,
after any deletions or
translations have taken place,
repeated sequences of the same
character will be replaced by one occurrence of the same character,
if the character is found in the array specified by the last operand.
If the last operand contains a character class, such as the
following example:
<pre>
<code>
tr -s '[:space:]'
</code>
</pre>
the last operand's array will contain all of the characters
in that character class.
However, in a case conversion, as described previously, such as:
<pre>
<code>
tr -s '[:upper:]' '[:lower:]'
</code>
</pre>
the last operand's array will contain only those characters
defined as the second characters in each of the
<b>toupper</b>
or
<b>tolower</b>
character pairs, as appropriate.
<p>
An empty string used for
<i>string1</i>
or
<i>string2</i>
produces undefined results.
</blockquote><h4><a name = "tag_001_014_2347">&nbsp;</a>EXIT STATUS</h4><blockquote>
The following exit values are returned:
<dl compact>

<dt>0<dd>All input was processed successfully.

<dt>&gt;0<dd>An error occurred.

</dl>
</blockquote><h4><a name = "tag_001_014_2348">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_001_014_2349">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
If necessary,
<i>string1</i>
and
<i>string2</i>
can be quoted to avoid pattern matching by the shell.
<p>
If an ordinary digit (representing itself) is to follow
an octal sequence, the octal sequence must use the
full three digits to avoid ambiguity.
<p>
When
<i>string2</i>
is shorter than
<i>string1</i>,
a difference results between historical System&nbsp;V and BSD systems.
A BSD system will pad
<i>string2</i>
with the last character found in
<i>string2.</i>
Thus, it is possible to do the following:
<pre>
<code>
tr 0123456789 d
</code>
</pre>
which would translate all digits to the letter
d.
Since this area is specifically unspecified in the
document, both the BSD and System&nbsp;V behaviours are
allowed, but a portable application cannot rely
on the BSD behaviour.
It would have to code the example in the following way:
<pre>
<code>
tr 0123456789 '[d*]'
</code>
</pre>
<p>
It should be noted that, despite similarities in appearance,
the string operands used by
<i>tr</i>
are not regular expressions.
<p>
Unlike some previous versions, the Issue 4
<i>tr</i>
correctly processes NUL characters in its input stream.
NUL characters can be stripped by using:
<pre>
<code>
tr -d '\000'
</code>
</pre>
<br>
</blockquote><h4><a name = "tag_001_014_2350">&nbsp;</a>EXAMPLES</h4><blockquote>
<ol>
<p>
<li>
The following example creates a list of all words in
<i>file1</i>
one per line in
<i>file2</i>,
where a word is taken to be a maximal string of letters.
<pre>
<code>
tr -cs "[:alpha:]" "[\n*]" &lt;file1 &gt;file2
</code>
</pre>
<br>
<p>
<li>
The next example translates all lower-case characters in
<b>file1</b>
to upper-case and writes the results to standard output.
<pre>
<code>
tr "[:lower:]" "[:upper:]" &lt;file1
</code>
</pre>
<p>
Note that the caveat expressed in the corresponding Issue 3
example is no longer in effect.
This case conversion is now a special case that employs the
<b>tolower</b>
and
<b>toupper</b>
classifications, ensuring that proper mapping is accomplished
(when the locale is correctly defined).
<p>
<li>
This example uses an equivalence class to identify accented variants
of the base character
e
in
<b>file1</b>,
which are stripped of diacritical marks and written to
<b>file2</b>.
<pre>
<code>
tr "[=e=]" e &lt;file1 &gt;file2
</code>
</pre>
<p>
</ol>
</blockquote><h4><a name = "tag_001_014_2351">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_2352">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="sed.html">sed</a></i>.
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
